<template>
  <el-dialog v-model="visible" :title="!dataForm.attachmentId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
          <el-form-item label="业务ID" prop="businessId">
        <el-input v-model="dataForm.businessId" placeholder="业务ID"></el-input>
      </el-form-item>
          <el-form-item label="业务表名" prop="tableName">
        <el-input v-model="dataForm.tableName" placeholder="业务表名"></el-input>
      </el-form-item>
          <el-form-item label="原始文件名称" prop="fileName">
        <el-input v-model="dataForm.fileName" placeholder="原始文件名称"></el-input>
      </el-form-item>
          <el-form-item label="附件存储名称" prop="fileSaveName">
        <el-input v-model="dataForm.fileSaveName" placeholder="附件存储名称"></el-input>
      </el-form-item>
          <el-form-item label="文件类型" prop="fileSuffix">
        <el-input v-model="dataForm.fileSuffix" placeholder="文件类型"></el-input>
      </el-form-item>
          <el-form-item label="附件的大小" prop="fileSize">
        <el-input v-model="dataForm.fileSize" placeholder="附件的大小"></el-input>
      </el-form-item>
          <el-form-item label="附件存储地址" prop="url">
        <el-input v-model="dataForm.url" placeholder="附件存储地址"></el-input>
      </el-form-item>
          <el-form-item label="附件的类型" prop="type">
            <ren-radio-group v-model="dataForm.type" dict-type="attment_type"></ren-radio-group>
<!--        <el-input v-model="dataForm.type" placeholder="附件的类型标识"></el-input>-->
      </el-form-item>
          <el-form-item label="附件的状态" prop="status">
            <ren-radio-group v-model="dataForm.status" dict-type="attment_status"></ren-radio-group>
<!--        <el-input v-model="dataForm.status" placeholder="附件的状态"></el-input>-->
      </el-form-item>
          <el-form-item label="备注说明" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注说明"></el-input>
      </el-form-item>
<!--          <el-form-item label="项目创建人" prop="createUser">-->
<!--        <el-input v-model="dataForm.createUser" placeholder="项目创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="项目创建时间，格式：YYYY-MM-DD HH:MM:SS" prop="createTime">-->
<!--        <el-input v-model="dataForm.createTime" placeholder="项目创建时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新人" prop="updateUser">-->
<!--        <el-input v-model="dataForm.updateUser" placeholder="最后更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" prop="updateTime">-->
<!--        <el-input v-model="dataForm.updateTime" placeholder="最后更新时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  attachmentId: '',  businessId: '',  tableName: '',  fileName: '',  fileSaveName: '',  fileSuffix: '',  fileSize: '',  url: '',  type: '',  status: '',  remark: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});

const rules = ref({
          businessId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          tableName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fileName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fileSaveName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fileSuffix: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fileSize: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          url: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          type: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          status: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          remark: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          createUser: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          createTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          updateUser: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          updateTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (attachmentId?: number) => {
  visible.value = true;
  dataForm.attachmentId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (attachmentId) {
    getInfo(attachmentId);
  }
};

// 获取信息
const getInfo = (attachmentId: number) => {
  baseService.get("/attachment/attachment/" + attachmentId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.attachmentId ? baseService.post : baseService.put)("/attachment/attachment", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
